<?php
/*********************************************************************
 * BUY CONTROLLER
 * 
 * Author: hthieu1110
 * Date: 12/04/2011 (generated automatically)
*********************************************************************/
require_once dirname(realpath(__FILE__))."./../../config/config.inc.php";

$adType = $_GET['type'];

$currentPage = (isset($_GET['p']) && $_GET['p'] > 0) ? $_GET['p'] : 1;
$formData = $_GET;

# Clean the data
$formData['town'] = trim($formData['town']);

# Get category list
$categoryId = (isset($formData['categoryId'])) ? $formData['categoryId'] : 0;
$category = new Category($categoryId);

# Get town info
$town = stripslashes($formData['town']);
$townId = Town::isTown($town);
# If townId = false then unset the townId to do not apply this 'where' in search query  
if ($townId == false){
  unset($townId);
  
  # If user enter just the towncode then search for the towncode
  if (is_numeric($town)) {
    $townCode = $town;
    
  # If user enter the random string then search for the townname
  } else {
    $townName = trim(htmlspecialchars($town, ENT_QUOTES));
  }
}

#advanced search------------------------------------------------------
#order by
if (isset($formData['orderBy'])) {
  list($orderBy, $orderDirection) = explode('_', $formData['orderBy']);
} else {
  $orderBy = 'createdAt';
  $orderDirection = 'desc';
}

#period
switch($formData['period']) {
  case 'day':
    $period = date('Y-m-d');
    break;
  case 'week':
    $period = date("Y-m-d", mktime(0, 0, 0, date('m'), date('d')-7, date('Y')));
    break;
  case 'month':
    $period = date("Y-m-d", mktime(0, 0, 0, date('m')-1, date('d'), date('Y')));
    break;
  case 'year':
    $period = date("Y-m-d", mktime(0, 0, 0, date('m'), date('d'), date('Y')-1));
    break;
  case 'all':
  default:
    unset($period);
    break;
}

# build the request and retrieve the advertisements
try {
  # connect to Db
  $Db = new Db();

  # if category is parent category then get list child category
  if ($category->isParentCategory()) {
    $childCategoryIds = $category->retrieveChildCategoryIds();
    $categoryId = implode(',', $childCategoryIds);
  }

  $where = array(
    'type'=>$adType,
  	'keyword'=>trim(nl2br(htmlspecialchars($formData['keyword'], ENT_QUOTES))),
    'categoryId'=>$categoryId,
    'townName'=>$townName,
    'townCode'=>$townCode,
    'townId'=>$townId,
    'period'=>$period
  );
  
  $order = array(
    'by'=>$orderBy,
    'direction'=>$orderDirection,
  );
  
  $limit = array(
    'begin'=>($currentPage-1)*MAX_ADS_PER_PAGE,
    'number'=>MAX_ADS_PER_PAGE,
  );
  
  $objAd = new Advertisement();
	$advertisements = $objAd->retrieveAdvertisements($where, $order, $limit);
  $countFoundAdvertisements = $objAd->countFoundAdvertisements();

} catch (Exception $e) { 
  if (ENV == "dev") {
    die($e->getMessage());
    
  } elseif (ENV == "prod") {
  	sessionSetVar('error', 'Gặp lỗi khi lấy thông tin về tin đăng từ cơ sở dữ liệu.');
  	header('Location:/error');
  	exit();
  }
}

require_once VIEW_PATH."header.inc.php";
require_once VIEW_PATH.'menu.inc.php';
require_once VIEW_PATH.'search.inc.php';
require_once VIEW_PATH."advertisement.inc.php";
require_once VIEW_PATH."footer.inc.php";
?>